package net.neptech.controller;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.neptech.dto.workflow.UserQueryDto;
import net.neptech.model.dto.*;
import net.neptech.model.sys.Dept;
import net.neptech.service.RemoteService;
import net.neptech.vm.ResponseResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 远程请求控制器
 */
@RestController
@RequestMapping("remote")
@RequiredArgsConstructor
public class RemoteController {

    private final RemoteService remoteService;

    /**
     * 根据用户id查询角色id集合
     */
    @GetMapping("loadRoleIdListByUserId")
    public ResponseResult<List<String>> loadRoleIdListByUserId(String userId) {
        return ResponseResult.success(remoteService.loadRoleIdListByUserId(userId));
    }

    /**
     * 根据部门id获取部门列表
     */
    @PostMapping("queryDeptList")
    public ResponseResult<List<Dept>> queryDeptList(@RequestBody List<String> deptIdList) {
        return ResponseResult.success(remoteService.queryDeptList(deptIdList));
    }

    /**
     * 保存待办任务
     */
    @PostMapping("saveMessage")
    public ResponseResult<Void> saveMessage(@RequestBody MessageDto messageDto) {
        remoteService.saveMessage(messageDto);
        return ResponseResult.success();
    }

    /**
     * 根据角色id集合查询用户id集合
     */
    @PostMapping("queryUserIdListByRoleIdList")
    public ResponseResult<List<String>> queryUserIdListByRoleIdList(@RequestBody List<String> roleIdList) {
        return ResponseResult.success(remoteService.queryUserIdsByRoleIds(roleIdList.stream().map(Long::parseLong).toList()).stream().map(String::valueOf).toList());
    }

    /**
     * 根据角色id集合和部门id集合查询人员id集合
     */

    @PostMapping("queryUserIdListByRoleIdListAndDeptIdList")
    public ResponseResult<List<String>> queryUserIdListByRoleIdListAndDeptIdList(@RequestBody UserQueryDto userQueryDto) {
        return ResponseResult.success(remoteService.queryUserIdsByRoleIdsAndDeptIds(userQueryDto).stream().map(String::valueOf).toList());
    }

    /**
     * 保存抄送
     */
    @PostMapping("saveCC")
    public ResponseResult<Void> saveCC(@RequestBody InstanceCopyDto copyDto) {
        remoteService.saveCC(copyDto);
        return ResponseResult.success();
    }

    /**
     * 检查是否是所有的父级
     */

    @PostMapping("checkIsAllParent")
    public ResponseResult<Boolean> checkIsAllParent(@RequestBody CheckParentDto checkParentDto) {
        return ResponseResult.success(remoteService.checkIsAllParent(checkParentDto));
    }


    /**
     * 根据部门id集合查询用户id集合
     */

    @PostMapping("queryUserIdListByDepIdList")
    public ResponseResult<List<String>> queryUserIdListByDepIdList(@RequestBody List<String> deptIdList) {
        return ResponseResult.success(remoteService.queryUserIdListByDepIdList(deptIdList.stream().map(Long::parseLong).toList()).stream().map(String::valueOf).toList());
    }

    /**
     * 检查是否是所有的子级
     */
    @PostMapping("checkIsAllChild")
    public ResponseResult<Boolean> checkIsAllChild(@RequestBody CheckChildDto checkChildDto) {
        return ResponseResult.success(remoteService.checkIsAllChild(checkChildDto));
    }

    /**
     * 获取用户的信息-包括扩展字段
     */
    @GetMapping("queryUserAllInfo")
    public ResponseResult<Map<String, Object>> queryUserAllInfo(String userId) {
        return ResponseResult.success(remoteService.queryUserAllInfo(userId));
    }


    /**
     * 查询上级部门
     */
    @SneakyThrows
    @GetMapping("queryParentDeptList")
    public ResponseResult<List<Dept>> queryParentDeptList(String deptId) {
        return ResponseResult.success(remoteService.queryParentDeptList(Long.parseLong(deptId)));
    }


    /**
     * 批量获取部门的下级部门
     */

    @PostMapping("batchQueryChildDeptList")
    public ResponseResult<Map<String, List<Dept>>> batchQueryChildDeptList(@RequestBody List<String> deptIdList) {
        return ResponseResult.success(remoteService.batchQueryChildDeptList(deptIdList.stream().map(Long::parseLong).toList()));
    }

    /**
     * 开始节点事件
     */
    @PostMapping("startNodeEvent")
    public ResponseResult<Void> startNodeEvent(@RequestBody InstanceNodeRecordParamDto recordParamDto) {
        remoteService.startNodeEvent(recordParamDto);
        return ResponseResult.success();
    }


    /**
     * 流程创建了
     */

    @PostMapping("processStartEvent")
    public ResponseResult<Void> processStartEvent(@RequestBody InstanceRecordParamDto processInstanceRecordParamDto) {
        remoteService.processStartEvent(processInstanceRecordParamDto);
        return ResponseResult.success();
    }

    /**
     * 结束节点事件
     */
    @PostMapping("endNodeEvent")
    public ResponseResult<Void> endNodeEvent(@RequestBody InstanceNodeRecordParamDto recordParamDto) {
        remoteService.endNodeEvent(recordParamDto);
        return ResponseResult.success();
    }

    /**
     * 取消节点事件
     */
    @PostMapping("cancelNodeEvent")
    public ResponseResult<Void> cancelNodeEvent(@RequestBody InstanceNodeRecordParamDto recordParamDto) {
        remoteService.cancelNodeEvent(recordParamDto);
        return ResponseResult.success();
    }

    /**
     * 开始设置执行人
     */

    @PostMapping("createTaskEvent")
    public ResponseResult<Void> createTaskEvent(@RequestBody InstanceAssignUserRecordParamDto processInstanceAssignUserRecordParamDto) {
        remoteService.createTaskEvent(processInstanceAssignUserRecordParamDto);
        return ResponseResult.success();
    }

    /**
     * 任务结束事件
     */

    @PostMapping("taskCompletedEvent")
    public ResponseResult<Void> taskCompletedEvent(@RequestBody InstanceAssignUserRecordParamDto processInstanceAssignUserRecordParamDto) {
        remoteService.taskCompletedEvent(processInstanceAssignUserRecordParamDto);
        return ResponseResult.success();
    }

    /**
     * 任务结束
     */

    @PostMapping("taskEndEvent")
    public ResponseResult<Void> taskEndEvent(@RequestBody InstanceAssignUserRecordParamDto processInstanceAssignUserRecordParamDto) {
        remoteService.taskEndEvent(processInstanceAssignUserRecordParamDto);
        return ResponseResult.success();
    }


    /**
     * 实例结束
     */

    @PostMapping("processEndEvent")
    public ResponseResult<Void> processEndEvent(@RequestBody InstanceParamDto processInstanceParamDto) {
        remoteService.processEndEvent(processInstanceParamDto);
        return ResponseResult.success();
    }


    /**
     * 查询流程管理员
     */
    @GetMapping("queryProcessAdmin")
    public ResponseResult<String> queryProcessAdmin(String flowId) {
        return ResponseResult.success(remoteService.queryProcessAdmin(flowId).toString()) ;
    }


    /**
     * 查询流程数据
     */
    @GetMapping("queryProcess")
    public ResponseResult<WorkflowDTO> queryProcess(String flowId) {
        return ResponseResult.success(remoteService.queryProcess(flowId));
    }

    /**
     * 保存节点数据
     */
    @PostMapping("saveNodeData")
    public ResponseResult<Void> saveNodeData(@RequestBody NodeDataDto processNodeDataDto) {
        remoteService.saveNodeData(processNodeDataDto);
        return ResponseResult.success();
    }

    /**
     * 保存执行数据
     */

    @PostMapping("saveExecution")
    public ResponseResult<Void> saveExecution(@RequestBody InstanceExecutionDto processInstanceExecutionDto) {
        remoteService.saveExecution(processInstanceExecutionDto);
        return ResponseResult.success();
    }

    /**
     * 获取节点数据
     */
    @GetMapping("getNodeData")
    public ResponseResult<String> getNodeData(String flowId, String nodeId) {
        return ResponseResult.success(remoteService.getNodeData(flowId, nodeId));
    }
}
